<?php if (!defined('TL_ROOT')) die('You cannot access this file directly!');

/**
 * Contao Open Source CMS
 * Copyright (C) 2005-2011 Leo Feyer
 *
 * Formerly known as TYPOlight Open Source CMS.
 *
 * This program is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation, either
 * version 3 of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program. If not, please visit the Free
 * Software Foundation website at <http://www.gnu.org/licenses/>.
 *
 * PHP version 5
 * @copyright  2011 Andreas Koob 
 * @author     Andreas Koob 
 * @package    tasks 
 * @license    LGPL 
 * @filesource
 */


/**
 * Table tl_tm_projects 
 */
$GLOBALS['TL_DCA']['tl_tm_projects'] = array
(

	// Config
	'config' => array
	(
		'dataContainer'               => 'Table',
		'enableVersioning'            => true,
		'ctable'                      => array('tl_tm_tasks'),
		'onsubmit_callback' => array
		(
			array('tl_tm_projects', 'processOnsubmit')
		),
		'onload_callback' => array
		(
			array('tl_tm_projects', 'processOnload')
		)
	),

	// List
	'list' => array
	(
		'sorting' => array
		(
			'mode'                    => 1,
			'fields'                  => array('project_order'),
			'disableGrouping'		  => true,
			'flag'                    => 1
		),
		'label' => array
		(
			'fields'                  => array('name'),
			'format'                  => '%s'
		),
		'global_operations' => array
		(
			'all' => array
			(
				'label'               => &$GLOBALS['TL_LANG']['MSC']['all'],
				'href'                => 'act=select',
				'class'               => 'header_edit_all',
				'attributes'          => 'onclick="Backend.getScrollOffset();" accesskey="e"'
			)
		),
		'operations' => array
		(
			'edit' => array
			(
				'label'               => &$GLOBALS['TL_LANG']['tl_tm_projects']['edit'],
				'href'                => 'table=tl_tm_tasks',
				'icon'                => 'edit.gif',
				'attributes'          => 'class="contextmenu"'
			),
			'editheader' => array
			(
				'label'               => &$GLOBALS['TL_LANG']['tl_tm_projects']['editheader'],
				'href'                => 'act=edit',
				'icon'                => 'header.gif',
				'button_callback'     => array('tl_tm_projects', 'editHeader'),
				'attributes'          => 'class="edit-header"'
			),
			'copy' => array
			(
				'label'               => &$GLOBALS['TL_LANG']['tl_tm_projects']['copy'],
				'href'                => 'act=copy',
				'icon'                => 'copy.gif'
			),
			'delete' => array
			(
				'label'               => &$GLOBALS['TL_LANG']['tl_tm_projects']['delete'],
				'href'                => 'act=delete',
				'icon'                => 'delete.gif',
				'attributes'          => 'onclick="if (!confirm(\'' . $GLOBALS['TL_LANG']['MSC']['deleteConfirm'] . '\')) return false; Backend.getScrollOffset();"'
			),
			'show' => array
			(
				'label'               => &$GLOBALS['TL_LANG']['tl_tm_projects']['show'],
				'href'                => 'act=show',
				'icon'                => 'show.gif'
			),
			'up' => array
			(
				'label'               => &$GLOBALS['TL_LANG']['tl_tm_projects']['up'],
				'href'                => 'act=up',
				'icon'                => 'up.gif'
			),
			'down' => array
			(
				'label'               => &$GLOBALS['TL_LANG']['tl_tm_projects']['down'],
				'href'                => 'act=down',
				'icon'                => 'down.gif'
			)
		)
	),

	// Palettes
	'palettes' => array
	(
		'__selector__'                => array(''),
		'default'                     => 'name,visibility;{access_rights},adv_creator_groups,owner_groups,cc_groups'
	),

	// Subpalettes
	'subpalettes' => array
	(
		''                            => ''
	),

	// Fields
	'fields' => array
	(
		'name' => array
		(
			'label'                   => &$GLOBALS['TL_LANG']['tl_tm_projects']['name'],
			'exclude'                 => true,
			'inputType'               => 'text',
			'eval'                    => array('mandatory'=>true, 'maxlength'=>255)
		),
		'visibility' => array
		(
			'label'                   => &$GLOBALS['TL_LANG']['tl_tm_projects']['visibility'],
			'exclude'                 => true,
			'inputType'               => 'select',
			'options'				  => array('E','C'),
			'reference'				  => &$GLOBALS['TL_LANG']['tl_tm_projects']['visibility']['reference'],
			'eval'                    => array('mandatory'=>true, 'maxlength'=>255)
		),
		'admin_groups' => array
		(
			'label'                   => &$GLOBALS['TL_LANG']['tl_tm_projects']['admin_groups'],
			'exclude'                 => true,
			'inputType'               => 'select',
			'foreignKey'              => 'tl_member_group.name',
			'eval'                    => array('mandatory'=>false,'multiple'=>true)
		),
		'owner_groups' => array
		(
			'label'                   => &$GLOBALS['TL_LANG']['tl_tm_projects']['owner_groups'],
			'exclude'                 => true,
			'inputType'               => 'select',
			'foreignKey'              => 'tl_member_group.name',
			'eval'                    => array('mandatory'=>true,'multiple'=>true)
		),
		'cc_groups' => array
		(
			'label'                   => &$GLOBALS['TL_LANG']['tl_tm_projects']['cc_groups'],
			'exclude'                 => true,
			'inputType'               => 'select',
			'foreignKey'              => 'tl_member_group.name',
			'eval'                    => array('mandatory'=>true,'multiple'=>true)
		)
	)
);

class tl_tm_projects extends Backend
{
	public function editHeader($row, $href, $label, $title, $icon, $attributes)
	{
		return  '<a href="'.$this->addToUrl($href.'&amp;id='.$row['id']).'" title="'.specialchars($title).'"'.$attributes.'>'.$this->generateImage($icon, $label).'</a> ';
	}
	
	public function processOnsubmit(DataContainer $dc)
	{
		if (!$dc->activeRecord)
		{
			return;
		}
		if($dc->activeRecord->comment_no=='0')
		{
			$arrProjects = $this->Database->prepare("SELECT max(project_order) AS max_project_order FROM tl_tm_projects")->execute();
			if ($arrProjects->numRows==0){
				$order_no=1;
			}
			else{
				$order_no= $arrProjects->max_commentno + 1;
			}
			$this->Database->prepare("UPDATE tl_lm_tasks SET project_order=? WHERE id=?")->execute($order_no, $dc->id);
		}
		
		//Delete old entries from project owner groups table
		$ret = $this->Database->prepare("DELETE FROM tl_tm_project_owner_groups WHERE project=?")->execute($dc->activeRecord->id);
		if(count($dc->activeRecord->owner_groups)>0)
		{		
			for($i=0;$i<=count($dc->activeRecord->owner_groups);$i++)
			{
				$ret=$this->Database->prepare("INSERT INTO tl_tm_project_owner_groups (project, owner_group) VALUES (?,?)")->execute($dc->activeRecord->id, $dc->activeRecord->owner_groups[$i]);
			}
		}
		
		//Delete old entries from project admin groups table
		$ret = $this->Database->prepare("DELETE FROM tl_tm_project_admin_groups WHERE project=?")->execute($dc->activeRecord->id);
		if(count($dc->activeRecord->admin_groups)>0)
		{		
			for($i=0;$i<=count($dc->activeRecord->admin_groups);$i++)
			{
				$ret=$this->Database->prepare("INSERT INTO tl_tm_project_admin_groups (project, admin_group) VALUES (?,?)")->execute($dc->activeRecord->id, $dc->activeRecord->admin_groups[$i]);
			}
		}
		
		//Delete old entries from project cc groups table
		$ret = $this->Database->prepare("DELETE FROM tl_tm_project_cc_groups WHERE project=?")->execute($dc->activeRecord->id);
		if(count($dc->activeRecord->cc_groups)>0)
		{		
			for($i=0;$i<=count($dc->activeRecord->cc_groups);$i++)
			{
				$ret=$this->Database->prepare("INSERT INTO tl_tm_project_cc_groups (project, cc_group) VALUES (?,?)")->execute($dc->activeRecord->id, $dc->activeRecord->cc_groups[$i]);
			}
		}
		return;
	}//public function processOnsubmit
	
	public function processOnload()
	{
		$id=$this->Input->get('id');
		$act=$this->Input->get('act');
		switch($act)
		{
			case "up":
				$currentProject=$this->Database->prepare("SELECT * FROM tl_tm_projects WHERE id=?")->execute($id);
				$previousProject=$this->Database->prepare("SELECT * FROM tl_tm_projects WHERE project_order<? ORDER BY project_order DESC LIMIT 0,1")->execute($currentProject->project_order);
				if ($previousProject->numRows > 0) //Is there a previous round?
				{
					//Update current project
					$this->Database->prepare("UPDATE tl_tm_projects SET project_order=? WHERE id=?")->execute($previousProject->project_order,$currentProject->id);
					//Update previous project
					$this->Database->prepare("UPDATE tl_tm_projects SET project_order=? WHERE id=?")->execute($currentProject->project_order,$previousProject->id);
				}
				//Go back to inital page. Otherwise contao will show a blank list
				$this->redirect('contao/main.php?do=tl_tm_projects&id=' . $currentProject->pid);
				break;
			case "down":
				$currentProject=$this->Database->prepare("SELECT * FROM tl_tm_projects WHERE id=?")->execute($id);
				$nextProject=$this->Database->prepare("SELECT * FROM tl_tm_projects WHERE project_order>? ORDER BY project_order ASC LIMIT 0,1")->execute($currentProject->project_order);
				if ($nextProject->numRows > 0) //Is there a previous round?
				{
					//Update current round
					$this->Database->prepare("UPDATE tl_tm_projects SET project_order=? WHERE id=?")->execute($nextProject->project_order,$currentProject->id);
					//Update previous round
					$this->Database->prepare("UPDATE tl_tm_projects SET project_order=? WHERE id=?")->execute($currentProject->project_order,$nextProject->id);
				}
				//Go back to inital page. Otherwise contao will show a blank list
				$this->redirect('contao/main.php?do=tl_tm_projects&id=' . $currentProject->pid);
				break;
		}//switch($act)
	}//public function processOnload
}
?>